/** The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License.
 *
 */
package tractorlk;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import java.awt.Color;

import tractorlk.drops.Board;
import tractorlk.drops.swing.DropsPanel;
import tractorlk.game.FluidGame;
import tractorlk.game.OngoingFluidGame;



public class Main {

        static int width;
        static int height;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final JFrame f = new JFrame("tractorlk - Kevin Woodmansee");
                width = 1024;
                height = 780;
		f.setSize(width, height);
		f.setLocation(150, 10);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

               // f.getContentPane().setBackground(Color.GREEN);

		final Board engine = new Board(new Dimension(1024, 780));

		final DropsPanel p = new DropsPanel(engine);

		JPanel mainP = new JPanel(new BorderLayout());
		mainP.add(p);
		f.getContentPane().add(mainP);

		final FluidGame g = new OngoingFluidGame();
		g.start(engine);



		final Timer timer1 = new Timer(66, new ActionListener() {

			public void actionPerformed(ActionEvent evt) {
				//g.run();
                            if(f.getSize() != new Dimension(width, height))
                            {
                                engine.resize(f.getSize());
                            }
				p.repaint();
			}

		});
		timer1.start();
		sleep();
		final Timer timer2 = new Timer(66, new ActionListener() {

			public void actionPerformed(ActionEvent evt) {
				//g.run();
				p.repaint();
			}

		});
		timer2.start();


                //***********************************
                 final JMenuBar menuBar = new JMenuBar();
                menuBar.setVisible(true);
		mainP.setFocusable(true);
		JMenu fileMenu = new JMenu("File");
		JMenu statsMenu = new JMenu("Stats");
		JMenu helpMenu = new JMenu("Help");
        JMenu settingsMenu = new JMenu("Settings");

		fileMenu.setMnemonic(KeyEvent.VK_F);
		statsMenu.setMnemonic(KeyEvent.VK_S);
		helpMenu.setMnemonic(KeyEvent.VK_H);
        settingsMenu.setMnemonic(KeyEvent.VK_E);


		JMenuItem fileNewGame = new JMenuItem("New Game", KeyEvent.VK_N);
		JMenuItem fileSaveGame = new JMenuItem("Save Game", KeyEvent.VK_S);
		JMenuItem fileLoadGame = new JMenuItem("Load Game", KeyEvent.VK_L);
		JMenuItem fileQuitGame = new JMenuItem("Quit Game", KeyEvent.VK_Q);

		JMenuItem statsHighScore = new JMenuItem("High Score", KeyEvent.VK_H);
		JMenuItem statsAchievements = new JMenuItem("Achievements", KeyEvent.VK_A);
		JMenuItem statsLifetime = new JMenuItem("Lifetime Stats", KeyEvent.VK_L);

		JMenuItem helpInstructions = new JMenuItem("Instructions", KeyEvent.VK_I);
		JMenuItem helpTutorial = new JMenuItem("Tutorial", KeyEvent.VK_T);
		JMenuItem helpCredits = new JMenuItem("Credits", KeyEvent.VK_C);
		JMenuItem helpAbout = new JMenuItem("About", KeyEvent.VK_A);

	    JMenuItem soundOn = new JMenuItem("Sound On", KeyEvent.VK_9);
	    JMenuItem soundOff = new JMenuItem("Sound Off", KeyEvent.VK_0);


		ActionListener fNG = new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				engine.restart();// placeholder for new game call

			}
		};

		// placeholder for save game call
//		ActionListener fSG = saveGame; 	// uses the listener from Debug Bar

		// placeholder for load game call
		//ActionListener fLG = loadGame;	// uses the listener from Debug Bar


		ActionListener fQG = new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(-1); // closes game
			}
		};
		ActionListener sHS = new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				// high score placeholder
				//engine.setScoreScreen();
			}
		};
		ActionListener sA = new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				// achievements placeholder
			}
		};
		ActionListener sL = new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				// lifetime stats placeholder
			}
		};
		ActionListener hI = new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				// instructions placeholder
			}
		};
		ActionListener hT = new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				//g.startTutorial(engine); // starts tutorial
			}
		};
		ActionListener hC = new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				// credits placeholder
			}
		};
		ActionListener hA = new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				// about placeholder
				//engine.setAboutScreen();
			}
		};
        ActionListener sOn = new ActionListener ()
        {
            public void actionPerformed( ActionEvent e ){
                //music.loopStart();
            }
        };
        ActionListener sOff = new ActionListener ()
        {
            public void actionPerformed( ActionEvent e ){
                //music.loopStop();
            }
        };

		fileNewGame.addActionListener(fNG);
		//fileSaveGame.addActionListener(fSG);
		//fileLoadGame.addActionListener(fLG);
		fileQuitGame.addActionListener(fQG);

		statsHighScore.addActionListener(sHS);
		statsAchievements.addActionListener(sA);
		statsLifetime.addActionListener(sL);

		helpInstructions.addActionListener(hI);
		helpTutorial.addActionListener(hT);
		helpCredits.addActionListener(hC);
		helpAbout.addActionListener(hA);

                soundOn.addActionListener(sOn);
                soundOff.addActionListener(sOff);


		fileMenu.add(fileNewGame);
		fileMenu.add(fileSaveGame);
		fileMenu.add(fileLoadGame);
		fileMenu.add(fileQuitGame);

		statsMenu.add(statsHighScore);
		statsMenu.add(statsAchievements);
		statsMenu.add(statsLifetime);

		helpMenu.add(helpInstructions);
		helpMenu.add(helpTutorial);
		helpMenu.add(helpCredits);
		helpMenu.addSeparator();
		helpMenu.add(helpAbout);

                settingsMenu.add(soundOn);
                settingsMenu.add(soundOff);

		menuBar.add(fileMenu);
		menuBar.add(statsMenu);
		menuBar.add(helpMenu);
                menuBar.add(settingsMenu);



		f.setJMenuBar(menuBar);

		/* Only shows the game once it's all set up. */
		f.setVisible(true);

		while(true)
		{
			mainP.grabFocus();
		}
                //*************************************
		

		//f.setVisible(true);
	}

	private static void sleep() {
		try {
			Thread.sleep(30); // put space between two threads.
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
	}

           
}
